package ws.jdbc.concurrent.driver.sqlserver.tds.datatypes;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * Fixed-Length Data Type.
 *
 * <p>
 * <a href="http://msdn.microsoft.com/en-us/library/dd341171">Reference</a>
 * </p>
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class TypeInfoFixedLenType extends TYPE_INFO {

  /**
   * Create new data type info instance.
   *
   * @param dataType fixed-length data type.
   */
  public TypeInfoFixedLenType(byte dataType) {
    this.dataType = dataType;
  }

  /**
   * Get the number of bytes of instance representation as a byte array.
   *
   * @return number of bytes of instance representation as a byte array.
   */
  @Override
  public int size() {
    return 1;
  }

  /**
   * Get the instance representation as a byte array.
   *
   * @return instance representation as a byte array.
   */
  @Override
  public byte[] getBytes() {
    return new byte[]{dataType};
  }

  /**
   * Read a fixed-length TYPE_INFO object from the input stream.
   *
   * @param is input stream.
   *
   * @return number of bytes read from the input stream.
   *
   * @throws IOException if an error occurs while reading from the data input stream.
   */
  @Override
  public int read(final ByteBuffer is) throws IOException {
    return 0;
  }

  /**
   * Since this {@code TYPE_INFO} if fixed-length, this will return {@code NULL}.
   * 
   * @return {@code NULL}.
   */
  @Override
  public TYPE_VARLEN getTypeVarLen() {
    return null;
  }

  /**
   * TYPE_INFO string representation.
   *
   * @return fixed-length TYPE_INFO fields as a string.
   */
  @Override
  public String toString() {
    return "TypeInfoFixedLenType{"
            + "fixedlentype=" + Integer.toHexString(0x0FF & dataType)
            + '}';
  }
}
